/**
 * 
 */
package org.apache.james.imap.server;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.LinkedList;

import org.apache.james.imapserver.ImapRequestLineReader;
import org.apache.james.imapserver.ProtocolException;

/**
 * @author zsombor
 *
 */
public class MinaImapMessage extends ImapRequestLineReader {

	@SuppressWarnings("unchecked")
	LinkedList messageFragments;
	String messageLine;
	int limit;
	int position;

	ByteBuffer buffer;
	
	public MinaImapMessage() {
	}

	public MinaImapMessage(String line) {
		this.messageLine = line;
		this.limit = messageLine.length();
		this.position = 0;
	}

	public void addLine(String line) {
		if (messageFragments==null) {
			messageFragments = new LinkedList();
		}
		messageFragments.add(line);
	}
	
	public void addBuffer(ByteBuffer buffer) {
		if (this.buffer==null) {
			this.buffer = buffer;
		} else {
			if (messageFragments==null) {
				messageFragments = new LinkedList();
			}
			messageFragments.add(buffer);
		}
	}
	
	
    
	/* (non-Javadoc)
	 * @see org.apache.james.imapserver.ImapRequestLineReader#commandContinuationRequest()
	 */
	public void commandContinuationRequest() throws ProtocolException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.apache.james.imapserver.ImapRequestLineReader#consume()
	 */
	public char consume() throws ProtocolException {
		if (position<limit) {
			char ch = messageLine.charAt(position);
			position++;
	        return ch;
		} else {
			return '\n';
		}
	}

	/* (non-Javadoc)
	 * @see org.apache.james.imapserver.ImapRequestLineReader#consumeLine()
	 */
	public void consumeLine() throws ProtocolException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.apache.james.imapserver.ImapRequestLineReader#eol()
	 */
	public void eol() throws ProtocolException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.apache.james.imapserver.ImapRequestLineReader#nextChar()
	 */
	public char nextChar() throws ProtocolException {
		return position<limit ? messageLine.charAt(position) : '\n';
	}

	/* (non-Javadoc)
	 * @see org.apache.james.imapserver.ImapRequestLineReader#nextWordChar()
	 */
	public char nextWordChar() throws ProtocolException {
		for (;position<limit && messageLine.charAt(position) == ' ';position++) {
			
		}
		return  position<limit ? messageLine.charAt(position) : '\n';
	}

	/* (non-Javadoc)
	 * @see org.apache.james.imapserver.ImapRequestLineReader#read(byte[])
	 */
	public void read(byte[] holder) throws ProtocolException {
		throw new ProtocolException("read is not supported!");
	}
	
	@Override
	public String consumeLiteral() throws ProtocolException {
		// 
		if (buffer==null) {
			throw new ProtocolException("Literal buffer is not found!");
		}
		String str = new String(buffer.array(), Charset.forName("ISO-8859-1"));
		if (messageFragments!=null && !messageFragments.isEmpty()) {
			messageLine = (String) messageFragments.removeFirst();
			this.limit = messageLine.length();
			if (!messageFragments.isEmpty()) {
				buffer = (ByteBuffer) messageFragments.removeFirst();
			}
		}
		
		return str;
	}
	
	@Override
	public String toString() {
		StringBuilder b = new StringBuilder("imap command:");
		b.append(messageLine);
		if (buffer!=null) {
			b.append('[').append(buffer.limit()).append(']');
		}
		if (messageFragments!=null) {
			for (Object obj : messageFragments) {
				if (obj instanceof String) {
					b.append(obj);
				}
				if (obj instanceof ByteBuffer) {
					b.append('[').append(((ByteBuffer)obj).limit()).append(']');
				}
			}
		}
		return b.toString();
	}

}
